/*EventCMD.java
 * 
 * This command creates a new event and adds it to a tour
 * making sure it does not conflict with the other tours
 *
 * Greg Warner
 * Derek Dupuis
 * Michael Dixey
 * Alex Schwartz
 */

import java.util.*;

public class EventCMD extends Cmd
{
   private String eventName;
   private Tour tour;
   private Provider prov;
   private int day, startTime, endTime;
   private float fitness;
   private String endPlace;
   public EventCMD() 
   { 
      super(); 
   }
   
   /*make new person in MedApp*/
   
   public void exec()
   {
      if( token.length >= 8 )
      {
         //try to format tokens to fields necessary
         try
         {
            eventName = token[ 1 ];
            prov = tourApp.getProvider( token[ 2 ] );
            tour = tourApp.getTour( token[ 3 ] );
            fitness = Float.parseFloat( token[ 4 ] );
            day = Integer.parseInt( token[ 5 ] );
            startTime = Integer.parseInt( token[ 6 ] );
            endTime = Integer.parseInt( token[ 7 ] );
         }
         catch ( NumberFormatException e ) //catch exception if bad token
         {
            System.err.print( "Event line has bad format!\n" );
            return;
         }
         
         
         //error checking
         if( prov == null )
         {
            System.err.print( "Provider does not exist!\n" );
            return;
         }
         
         if( tour == null )
         {
            System.err.print( "Tour does not exist!\n" );
            return;
         }
         
         if( prov.capacity() < tour.capacity() )
         {
            System.err.print( "Capacity of new event too low for tour!\n" );
            return;
         }
         
         if( fitness < 0 )
         {
            System.err.print( "Fitness level is negative!\n" );
            return;
         }
         
         //check for bad time format
         if( day < 0 || startTime >= endTime || startTime < 0 ||
            startTime >= 2400 || endTime < 0 || endTime >= 2400 )
         {
            System.err.print( "Bad time format for event!\n" );
            return;
         }
         
         if( tour.locked() )
         {
            System.err.println( "Tour is locked, no events are allowed " +
                               " to be added." );
            return;
         }
         
         //check if event is a travel, if so it needs an end place
         if( prov.service().type().compareTo( "travel" ) == 0 )
         {
            if( token.length >= 9 )
            {
               endPlace = token[ 8 ];
            }
            else
            {
               System.err.print( "Service is travel " + 
                                "but no end place specified!\n" );
               return;
            }
         }
         else //if not travel end place is the start place
            endPlace = prov.name();
         
         //creates the new event
         Event event = new Event( prov.name(), endPlace, startTime, 
                                 endTime, fitness, day, eventName,
                                 tour, prov );
         
         if( !tour.events().conflict( event ) )
         {
            tour.addEvent( event );
         }
         else
         {
            System.err.print( "Event conflicts with provider's schedule or" +
                             " with tour's schedule!\n" );
            return;
         }
      }
      else
      {
         System.err.print( "Not enough tokens on event line!\n" );
         return;
      }
   }
}